package com.gztms.utils;

import com.gztms.usermgnt.pojo.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.List;

/**
 * @author Zoey
 * @date 2023/5/2 13:38
 * @Description TODO
 **/
@Slf4j
public class SecurityUtils {
    /**
     * 获取用户
     * @return
     */
    public static LoginUser getLoginUser() {
        return (LoginUser) getAuthentication().getPrincipal();
    }

    /**
     * 获取authentication
     * @return
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isAdmin() {

        List<String> permissions = getLoginUser().getPermissions();
//        System.out.println(permissions);
        if (permissions.contains("ROLE_ADMIN")) {
            return true;
        }
        // 查询角色
        return false;

    }

    public static String getUid() {
        return getLoginUser().getUser().getUid();
    }
}
